<?php
namespace App\Repositories;

use App\Models\AdminUser;
use Illuminate\Support\Facades\Hash;

class AdminUserRepository
{
	public function getUsers()
    {
        return AdminUser::with('roles')->get();
    }

    public function getUser($id)
    {
        return AdminUser::with('roles')->find($id);
    }

    public function addUser($request)
    {
        $User = AdminUser::create([
            'user_name'   => $request->user_name,
            'phone'       => $request->phone,
            'email'       => $request->email,
            'description' => $request->description,
            'status'      => isset($request->status) ? $request->status : 1,
            // 'password' => password_hash('123456', PASSWORD_DEFAULT),
            'password'    => Hash::make('123456'),
            'is_super'    => 0,
        ]);

        //添加角色
        $User->roles()->attach($request->roles);

        return $User;
    }

    public function updateUser($request, $id)
    {
        $User = AdminUser::findOrFail($id);

        $User->user_name   = $request->user_name;
        $User->phone       = $request->phone;
        $User->email       = $request->email;
        $User->description = $request->description;
        $User->status      = $request->status;

        $User->save();

        $User->roles()->sync($request->roles);

    }

    public function deleteUser($id)
    {
        return AdminUser::find($id)->delete();
    }

    public function deleteUserLists($ids)
    {
        return AdminUser::whereIn('id',$ids)->delete();
    }
}